﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nixtla
{
    namespace TimeTracker
    {
        public interface ICategory
        {
            string Name
            {
                get;
                set;
            }
            ICategory Parent
            {
                get;
                set;
            }
            IEnumerable<ICategory> Children
            {
                get;
            }
            string QualifiedName
            {
                get;
            }
        }

        public class Category : ICategory
        {
            private string name;
            private Category parent;
            private List<ICategory> children;

            public Category()
            {
                name = "New Category";
                parent = null;
                children = new List<ICategory>();
            }

            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }

            public ICategory Parent
            {
                get
                {
                    return parent;
                }
                set
                {
                    Category oldParent = parent;
                    parent = value as Category;
                    if (null != parent)
                    {
                        parent.children.Add(this);
                    }
                    if (null != oldParent)
                    {
                        oldParent.children.Remove(this);
                    }
                }
            }

            public IEnumerable<ICategory> Children
            {
                get
                {
                    return children.AsEnumerable<ICategory>();
                }
            }

            public string QualifiedName
            {
                get
                {
                    string parentQualifiedName = "";
                    if (null != parent)
                    {
                        parentQualifiedName = parent.QualifiedName;
                    }
                    return parentQualifiedName + "\\" + name;
                }
            }
        }
    }
}
